<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      matrix = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
      ];
      matrix = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
      ];
      var spiralOrder = function (matrix) {
        if (matrix.length == 0) return [];
        let res = [];
        res.push(...matrix[0]);
        for (let i = 1; i < matrix.length; i++) {
          res.push(matrix[i][matrix[i].length - 1]);
        }

        for (let i = matrix[0].length - 2; i >= 0; i--) {
          res.push(matrix[matrix.length - 1][i]);
        }
        console.log(res);
        return res;
      };
      spiralOrder(matrix);
    </script>
  </body>
</html>
